package com.group1.bookmanagement.util;

import java.util.Random;

/**
 * 字符串生成工具
 */
public class RandomCharacter {

	// 基本字符
	private static final char[] BASIC_CHAR = new char[] {
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z', 
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	private static final Random random = new Random();

	/**
	 * 随机生成一个字符
	 * @return
	 */
	public static char random() {
		int index = random.nextInt(BASIC_CHAR.length);
		return BASIC_CHAR[index];
	}

	/**
	 * 生成指定长度的字符串
	 * @param num
	 * @return
	 */
	public static String random(int len) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < len; i++) {
			builder.append(random());
		}

		return builder.toString();
	}

	/**
	 * 随机生成文件名
	 * @param oldName 文件原名
	 * @return
	 */
	public static String randomName(String oldName) {
		// 截取文件的后缀名
		String suffix = oldName.substring(oldName.lastIndexOf("."));
		StringBuilder builder = new StringBuilder();
		//文件名长度在12-32之间
		int length = random.nextInt(20) + 12;
		builder.append(random(length))
			.append(suffix);

		return builder.toString();
	}

}
